package com.shouer.doc.ui.billlist.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.shouer.doc.R;
import com.shouer.doc.common.util.AppUtil;
import com.shouer.doc.common.util.DateFormatUtil;
import com.shouer.doc.common.util.RelativeDateFormatUtil;
import com.shouer.doc.ui.userinfo.model.BillModel;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ZXL on 2016/12/20.
 */

public class BillListAdapter extends RecyclerView.Adapter<BillViewHolder> {

    private Context context;
    private List<BillModel> billModels;
    private LayoutInflater inflater;

    public BillListAdapter(Context context)
    {
        this.context=context;
        inflater=LayoutInflater.from(context);
    }

    @Override
    public BillViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView=inflater.inflate(R.layout.activity_billlist_item_layout,null);
        return new BillViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(BillViewHolder holder, int position) {
        BillModel billModel = billModels.get(position);
        holder.billDescView.setText(billModel.getBillDesc());
        try {
            holder.billTimeView.setText(RelativeDateFormatUtil.format(DateFormatUtil.stringToDate(billModel.getBillTime().replace("T", "\b"), "yyyy-MM-dd\bHH:mm:ss")));
        } catch (Exception e) {
            Log.d(BillListAdapter.class.getSimpleName(), e.toString());
            holder.billTimeView.setText("");
        }
        if (billModel.getBillType().startsWith("1")) {
            holder.billAmountView.setText("+" + billModel.getBillAmount());
        } else {
            holder.billAmountView.setText("-" + billModel.getBillAmount());
        }

        String icoUriString = null;
        if (billModel.getBillType().startsWith("1")) {
            if (billModel.getBillType().equals("11") || billModel.getBillType().equals("11")) {
                icoUriString = "res://" + AppUtil.getPackageName(context) + "/" + R.drawable.ico_billtype_tw;
            } else {
                icoUriString = "res://" + AppUtil.getPackageName(context) + "/" + R.drawable.ico_billtype_dh;
            }
        } else {
            icoUriString = "res://" + AppUtil.getPackageName(context) + "/" + R.drawable.ico_billtype_tx;
        }
        holder.billTypeView.setImageURI(icoUriString);
    }

    @Override
    public int getItemCount() {
        if (null == billModels) {
            return 0;
        } else {
            return billModels.size();
        }
    }

    public void refreshBillList(List<BillModel> billModels) {
        this.billModels = billModels;
        notifyDataSetChanged();
    }

    public void attachToBillList(List<BillModel> billModels1) {
        if (null != billModels1) {
            if (null == billModels) {
                billModels = new ArrayList<>(0);
            }

            billModels.addAll(billModels1);
            notifyDataSetChanged();
        }
    }
}
